package com.itcast.googleplay.adapter;

import java.util.List;

import com.itcast.googleplay.R;
import com.itcast.googleplay.http.HttpApi;
import com.itcast.googleplay.utils.UIUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.graphics.Bitmap;

public class MyPagerAdapter extends PagerAdapter {
	public List<String> list;
	private DisplayImageOptions options;
	public MyPagerAdapter(List<String> list) {
		this.list = list;
		
		options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.ic_default)
			.showImageForEmptyUri(R.drawable.ic_default)
			.showImageOnFail(R.drawable.ic_default)
			.imageScaleType(ImageScaleType.EXACTLY)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)//是否允许让图片反转
			.build();
	}
	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return  view == object;
	}
	
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		ImageView imageView = new ImageView(UIUtils.getContext());
		ImageLoader.getInstance().displayImage(HttpApi.IMAGEURL+list.get(position%list.size()), imageView, options);
		container.addView(imageView);
		return imageView;
	}
	
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View)object);
	}
}
